﻿using Core.DataAccess.Model;
using Core.DataAccess.Model.iTool.Gateway;
using HandlerContextOptions;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using UserCenter.Interfaces;

namespace UserCenter.Implements
{
    public class RoleBasics : IRoleBasics
    {
        public async Task<RoleModifyResult> ModifyAsync(RoleModifyRequset requset)
        {
            await using (iToolGatewayContext context = new iToolGatewayContext())
            {
                context.SysRoles.Update(requset);
                int row = await context.SaveChangesAsync();
                if (requset.RoleId > 0)
                {
                    var roleMenu = context.SysRoleMenus.Where(item => item.RoleMenuRoleId == requset.RoleId).FirstOrDefault();
                    if (roleMenu == null)
                    {
                        await context.SysRoleMenus.AddAsync(new SysRoleMenu()
                        {
                            RoleMenuRoleId = requset.RoleId,
                            RoleMenuMenuIds = string.IsNullOrEmpty(requset.RoleMenuId) ? null : $",{requset.RoleMenuId},",
                            RoleMenuCreateTime = DateTime.Now
                        });
                    }
                    else
                    {
                        roleMenu.RoleMenuMenuIds = string.IsNullOrEmpty(requset.RoleMenuId) ? null : $",{requset.RoleMenuId},";
                    }

                    row = await context.SaveChangesAsync();
                }
                return new RoleModifyResult
                {
                    IsSuccessful = row > 0,
                };
            }
        }

        public async Task<RoleRegisterResult> RegisterAsync(RoleRegisterRequset requset)
        {
            await using (iToolGatewayContext context = new iToolGatewayContext())
            {
                try
                {
                    await context.SysRoles.AddAsync(requset);
                    await context.SaveChangesAsync();

                    if (requset.RoleId > 0)
                    {
                        var roleMenu = context.SysRoleMenus.Where(item => item.RoleMenuRoleId == requset.RoleId).FirstOrDefault();
                        if (roleMenu == null)
                        {
                            await context.SysRoleMenus.AddAsync(new SysRoleMenu()
                            {
                                RoleMenuRoleId = requset.RoleId,
                                RoleMenuMenuIds = string.IsNullOrEmpty(requset.RoleMenuId) ? null : $",{requset.RoleMenuId},",
                                RoleMenuCreateTime = DateTime.Now
                            });
                        }
                        else
                        {
                            roleMenu.RoleMenuMenuIds = string.IsNullOrEmpty(requset.RoleMenuId) ? null : $",{requset.RoleMenuId},";
                        }

                        await context.SaveChangesAsync();
                    }
                    return new RoleRegisterResult
                    {
                        IsSuccessful = true
                    };
                }
                catch (Exception ex)
                {
                    return new RoleRegisterResult
                    {
                        IsSuccessful = false,
                        Description = ex.Message
                    };
                }
            }
        }
        public async Task<RoleDeleteResult> Delete(RoleDeleteRequset requset)
        {
            await using (iToolGatewayContext context = new iToolGatewayContext())
            {
                var role = await context.SysRoles.FindAsync(requset.RoleId);
                if (role == null)
                {

                    return new RoleDeleteResult
                    {
                        IsSuccessful = false,
                        Description = "角色不存在"
                    };
                }
                else
                {
                    context.SysRoles.Remove(role);
                    try
                    {
                        var rolemenu = context.SysRoleMenus.Where(a => a.RoleMenuRoleId == requset.RoleId).FirstOrDefault();
                        if (rolemenu != null)
                        {
                            context.SysRoleMenus.Remove(rolemenu);
                        }
                        await context.SaveChangesAsync();

                        return new RoleDeleteResult
                        {
                            IsSuccessful = true
                        };
                    }
                    catch (Exception ex)
                    {
                        return new RoleDeleteResult
                        {
                            IsSuccessful = false,
                            Description = ex.Message
                        };
                    }
                }
            }
        }
        public async Task<List<SysRole>> GetSysRoles(Pagination pagination)
        {
            await using (iToolGatewayContext context = new iToolGatewayContext())
            {
                return await context
                               .SysRoles
                               .Where(item => item.RoleIsDelete != 1).PaginationAsync(pagination);
            }
        }
        public async Task<List<SysUserRole>> GetSysUserRoles()
        {
            await using (iToolGatewayContext context = new iToolGatewayContext())
            {
                return await context
                              .SysUserRoles.ToListAsync();
            }
        }
    }
}
